package com.example.shoppingdemo.nslcommodity.adapter;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.nslcommodity.javabean.ShopModel;
import com.example.shoppingdemo.nslcommodity.myview.RoundRectImageView;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ShopHolder>{
    private Context context;
    ViewGroup group;
    public ShopAdapter(Context context) {
        this.context = context;
    }
    private List<ShopModel>shopModels;
    public ShopAdapter(List<ShopModel> data) {
        this.shopModels = data;
    }
    public void setShopModels(List<ShopModel> shopModels) {
        this.shopModels = shopModels;
    }
    @NonNull
    @Override
    public ShopHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        group=parent;
        ShopHolder holder=new ShopHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.shoppng_item,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ShopAdapter.ShopHolder holder, int position) {
        Uri uri=Uri.parse(shopModels.get(position).getIcon());
        Glide.with(group.getContext()).load(uri).into(holder.icon);
        holder.title.setText(shopModels.get(position).getTitle());
        JSONArray jsonArray=JSONArray.fromObject(shopModels.get(position).getSku());
        StringBuilder sbr=new StringBuilder();
        if (jsonArray.size()!=0){
            for (int i = 0; i <jsonArray.size() ; i++) {
                JSONObject jo = JSONObject.fromObject (jsonArray.get (i));
                Collection values = jo.values();
                if (values.size()>=2){
                    sbr= sbr.append(values.toArray()[0]+","+values.toArray()[1]+";");
                }
            }
        }
        holder.sku.setText(sbr.toString());
        holder.money.setText("￥"+shopModels.get(position).getMoney());
        holder.numder.setText("X"+shopModels.get(position).getNumder());
    }
    @Override
    public int getItemCount() {
        return shopModels==null?0:shopModels.size();
    }

    class ShopHolder extends RecyclerView.ViewHolder{
        RoundRectImageView icon;
        TextView title;
        TextView sku;
        TextView money;
        TextView numder;
        public ShopHolder(@NonNull View itemView) {
            super(itemView);
            icon=itemView.findViewById(R.id.image_order_form_all_item);
            title=itemView.findViewById(R.id.text_all_item_title);
            sku=itemView.findViewById(R.id.text_all_item_sku);
            money=itemView.findViewById(R.id.text_all_item_money);
            numder=itemView.findViewById(R.id.text_all_item_numder);
        }
    }
}
